<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="trainCode">
        <el-input
          v-model="queryParams.trainCode"
          placeholder="请选择车次"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="date">
        <el-date-picker clearable
          v-model="queryParams.date"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查找</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:sk-token:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:sk-token:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:sk-token:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:sk-token:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 使用折叠面板展示分组数据 -->
    <el-collapse v-loading="loading">
      <el-collapse-item v-for="(group, index) in groupedTokens" :key="index" :name="index">
        <template slot="title">
          <div class="train-group-title">
            <span class="train-code">车次: {{ group.trainCode }}</span>
          </div>
        </template>
        
        <!-- 嵌套表格显示具体令牌信息 -->
        <el-table :data="group.tokens" :row-style="{ height: '60px' }" @selection-change="handleSelectionChange">
          <el-table-column show-overflow-tooltip type="selection" width="55" align="center" />
          <el-table-column show-overflow-tooltip label="序号" type="index" width="50" align="center" />
          <el-table-column show-overflow-tooltip label="id" align="center" prop="id" />
          <el-table-column show-overflow-tooltip label="日期" align="center" prop="date" width="150">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="车次编号" align="center" prop="trainCode" width="100"/>
          <el-table-column show-overflow-tooltip label="令牌余量" align="center" prop="count" width="100">
            <template slot-scope="scope">
              <el-tag :type="scope.row.count > 0 ? 'success' : 'danger'">{{ scope.row.count }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['business:sk-token:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['business:sk-token:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-collapse-item>
    </el-collapse>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改令牌余量对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车次编号" prop="trainCode">
          <el-input v-model="form.trainCode" placeholder="请输入车次编号" />
        </el-form-item>
        <el-form-item label="令牌余量" prop="count">
          <el-input-number v-model="form.count" :min="0" placeholder="请输入令牌余量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSkTokenByGroup, getSkToken, delSkToken, addSkToken, updateSkToken } from "@/api/business/sk-token";

export default {
  name: "SkToken",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 令牌余量分组数据
      groupedTokens: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        date: null,
        trainCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        trainCode: [
          { required: true, message: "车次编号不能为空", trigger: "blur" }
        ],
        count: [
          { required: true, message: "令牌余量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询令牌余量列表 */
    getList() {
      this.loading = true;
      listSkTokenByGroup(this.queryParams).then(response => {
        this.groupedTokens = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        date: null,
        trainCode: null,
        count: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加令牌余量";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSkToken(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改令牌余量";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSkToken(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSkToken(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除令牌余量编号为"' + ids + '"的数据项？').then(function() {
        return delSkToken(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/sk-token/export', {
        ...this.queryParams
      }, `sk-token_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
/* 折叠面板相关样式 */
.train-group-title {
  display: flex;
  align-items: center;
  font-size: 14px;
}

.train-code {
  font-weight: bold;
  margin-right: 20px;
  min-width: 80px;
}

.token-count {
  margin-right: 20px;
  min-width: 120px;
}

.token-date {
  color: #606266;
}

.el-collapse-item__header {
  height: 60px;
  line-height: 60px;
  background-color: #f5f7fa;
  padding-left: 15px;
}

.el-collapse-item__content {
  padding: 10px;
}

.el-tag--success {
  background-color: rgba(103, 194, 58, 0.1);
  color: #67c23a;
}

.el-tag--danger {
  background-color: rgba(245, 108, 108, 0.1);
  color: #f56c6c;
}
</style>